define(function(require, exports, module) {
	'use strict';
    var rk = require('rk');
    
    
    
    $.widget('rk.logoSetting', $.rk.oaDialogCtrl, {
            _create: function(){
                var me = this;
                var elem = me.element; 

                var tpl = require('./logoSetting.tpl');
                elem.html(rk.templateText(tpl));
            },
            oldLogoUrl: null,
            _init: function(){
                var me = this;
                var elem = me.element;


                var dialog = elem.closest('.ui-dialog');
                dialog.find('.ui-dialog-titlebar-close').click(function(){
                    me._rollback();
                });

                me.droparea = elem.find('.upload-btn');

                me.oldLogoUrl = $('#pageleft').pageLeftCtrl('getLogo');
                if(!me._isDefault(me.oldLogoUrl)){
                    me._showUploadedImage(me.oldLogoUrl);
                    elem.find('a[act="bedefault"]').show();
                    elem.find('a[act="reupload"]').show();
                }

                elem.find('.upload-btn, .reupload-btn').fileupload({
                    dropZone: me.droparea,
                    dataType: 'json',
                    pasteZone: me.droparea,
                    paramName: 'uploadFile',
                    limitMultiFileUploads: 9,
                    limitMultiFileUploadSize: '5 MB',
                    formData: function(form) {
                        var file = this.files[0];
                        return [{
                            name: 'uploadFileName',
                            value: file.name
                        }, {
                            name: 'pageTenantId',
                            value: window.SESSION.tenantId
                        }]
                    },
                    url: '/json/global_uploadify/img-upload.action',

                    add: function(e, data) {
                        if (e.isDefaultPrevented()) {
                            return false;
                        }
                        if (!/\.(jpg|jpeg|gif|png)$/i.test(data.files[0].name)) {
                            return false;
                        }
                        data.process().done(function() {
                                me._beLoading(true);
                                data.submit();
                            });
                    },
                    send: function(e, data) {

                    },
                    done: function(e, data) {
                        me._unLoading();
                        var result = data.result;
                        if (result.status == 0) {
                            me.currentResult = result;
                            var params = result.data.returnParams;
                            me._showUploadedImage(params.fileLUrl);
                            $('#pageleft').pageLeftCtrl('setLogo', params.fileLUrl);
                            if(!me._isDefault(me.oldLogoUrl))elem.find('a[act="bedefault"]').show();
                            elem.find('a[act="reupload"]').show();
                        } else {
                            var $el = $('#' + data.uid);
                            $el.find('span.load').addClass('load_failure');
                        }
                    }
                });
                //还原默认logo
                elem.find('a[act="bedefault"]').click(function(){
                    var fileUrl = me._getDefaultLogo();
                    $('#pageleft').pageLeftCtrl('setLogo', fileUrl);
                    me.currentResult = {
                        data: {
                            returnParams:{
                                fileUrl: ''//空就是默认
                            }
                        }
                    }
                    me._showUploadedImage(fileUrl);
                    $(this).hide();
                });
                //保存
                me.getBtn('save').on('click', function (){
                    //if(window._hmt){_hmt.push(['_trackEvent', 'operate', 'logoSet', window.SESSION.tenantName]);} //baidu 统计
                    if(!me.currentResult)return;
                    var logoUrl = me.currentResult.data.returnParams.fileUrl;
                    var data = {
                            paramKey:'LOGO_SET',
                            paramValue: logoUrl
                        };
                    rk.post("/json/crm_commonParam/save.action", data).done(function(data) {
                        $('#pageleft').pageLeftCtrl('setLogo', logoUrl ? logoUrl : me._getDefaultLogo());
                        me.logoUpdated = true;
                        me.closeMe();
                    });
                });
                //取消
                me.getBtn('cancel').on('click', function (){
                    me.closeMe();
                });
            },
            _getDefaultLogo: function(){
                var themeName = $.trim(SESSION.navValue) ? SESSION.navValue : 'theme-default';
                return '/static/img/v3.0/' + rk.replaceStr(themeName, '-', '_') + '_logo.png';
            },
            _beLoading: function(){
                var me = this;
                var elem = me.element;
                var uploadBtn = $('.upload-btn');
                var left = uploadBtn.position().left + uploadBtn.width() / 2;
                var top = uploadBtn.position().top + uploadBtn.height() / 2;
                elem.append('<img act="loading" style="position:absolute;left:'+left+'px;top:'+top+'px;" src="'+SESSION.resDomain+'/static/img/v3.0/loading.gif" style="" class="mCS_img_loaded">');
            },
            _unLoading: function(){
                var me = this;
                var elem = me.element;
                elem.find('img[act="loading"]').remove();
            },
            _showUploadedImage: function(url){
                var me = this;
                var elem = me.element;

                var menuBgColor = $('#pageleft').find('.nav-menu').css('background-color');

                if(me._isDefault(url)) elem.find('.upload-btn').css('background-color', menuBgColor);
                me.droparea.find('.upload-button-text, img').remove();
                me.droparea.append('<img src="'+url+'" style="width: 120px;height:40px;">');
            },
            currentResult: null,
            _rollback: function(){
                var me = this;
                if(me.oldLogoUrl && me.oldLogoUrl != $('#pageleft').pageLeftCtrl('getLogo')){
                    $('#pageleft').pageLeftCtrl('setLogo', me.oldLogoUrl);
                    me.closeMe();
                }
            },
            _destroy: function(){
                var me = this;
                if(me.logoUpdated)return;
                if(me.oldLogoUrl && me.oldLogoUrl != $('#pageleft').pageLeftCtrl('getLogo')){
                    $('#pageleft').pageLeftCtrl('setLogo', me.oldLogoUrl);
                    me.closeMe();
                }
            },
            _isDefault: function(url){
                var reg = new RegExp(/[\w\W]*\/theme_([\w]*)_logo.png/g);
                return reg.test(url);
            },
            _isEqual: function(url1, url2){
                url1 = rk.replaceStr(url1, '/', '').toLowerCase();
                url2 = rk.replaceStr(url2, '/', '').toLowerCase();
                return url1 == url2;
            }
    });
});